package com.example.gobang.API;

import com.example.gobang.game.ChattererRequest;
import com.example.gobang.game.OnlineUserManager;
import com.example.gobang.game.Room;
import com.example.gobang.game.RoomManager;
import com.example.gobang.model.UserInfo;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.WebSocketMessage;
import org.springframework.web.socket.WebSocketSession;


@Component
public class ChatAPI implements WebSocketHandler {

    @Resource
    RoomManager roomManager;

    ObjectMapper objectMapper = new ObjectMapper();

    @Autowired
    OnlineUserManager onlineUserManager;
    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        System.out.println("ChatAPI::{afterConnectionEstablished}成功建立!");
        UserInfo userInfo = (UserInfo) session.getAttributes().get("userInfo");
        onlineUserManager.addCharUsers(userInfo.getUserId(), session);
    }

    @Override
    public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception {
        System.out.println("接收到ChatAPI：：message："+ message.getPayload());
        // 1.首先接收到当前对象
        UserInfo userInfo = (UserInfo) session.getAttributes().get("userInfo");
        if (userInfo == null) {
            System.out.println("ChatAPI::userInfo不存在!" );
            return;
        }

//        2.获取游戏房间并且接收前端发送来的数据
        ChattererRequest chattererRequest = objectMapper.readValue(message.getPayload().toString(), ChattererRequest.class);
        if (chattererRequest == null || !chattererRequest.getChatSign().equals("chat")) {
            System.out.println("chattererRequest 为空");
            return;
        }

        Room room = roomManager.getByUserId(userInfo.getUserId());
        chattererRequest.setUserName(userInfo.getUserName());

        // 存储并且展示
        room.displayChatMes(chattererRequest);

    }

    @Override
    public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
        System.out.println("ChatAPI::{handleTransportError}连接异常!");
        UserInfo userInfo = (UserInfo) session.getAttributes().get("userInfo");
        WebSocketSession onlineSession = onlineUserManager.getChatUsers(userInfo.getUserId());
        if (session == onlineSession) {
            onlineUserManager.exitChatUsers(userInfo.getUserId());
        }
    }

    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception {
        System.out.println("ChatAPI::{afterConnectionClosed}连接关闭!");
        UserInfo userInfo = (UserInfo) session.getAttributes().get("userInfo");
        WebSocketSession onlineSession = onlineUserManager.getChatUsers(userInfo.getUserId());
        if (session == onlineSession) {
            onlineUserManager.exitChatUsers(userInfo.getUserId());
        }
    }

    @Override
    public boolean supportsPartialMessages() {
        return false;
    }
}
